<?php
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use think\facade\Request;

/**
 * Class AdminIndexController
 * @package app\tenant\controller
 * @adminMenuRoot(
 *     'name'   =>'系统参数管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10001,
 *     'icon'   =>'cog',
 *     'remark' =>'系统参数管理'
 * )
 */
class AdminSystemController extends AdminBaseController
{
    /**
     * 系统参数列表
     * @adminMenu(
     *     'name'   => '系统参数列表',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '系统参数列表',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index(){
    	$param = $this->request->param();
        $isPage=false;
        $data = Db::name('system_setting')
            ->where(function (Query $query) use ($param,$isPage ) {

                $keyword = empty($param['keyword']) ? '' : $param['keyword'];
                if (!empty($keyword)) {
                    $query->where('desc', 'like', "%$keyword%");
                }

            })
            ->order('id', 'ASC')
            ->paginate(10);

        $data->appends($param);

        $this->assign('page', $data->render());
        $this->assign('params',$data);
        return $this->fetch();
    }
    /**
     * 系统参数添加
     * @adminMenu(
     *     'name'   => '系统参数添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '系统参数添加',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add(){
    	$param = $this->request->param();
    	if(Request::isPost()){
    		$data = array(
    			'desc' => $param['desc'],
    			'key_name' => $param['key_name'],
    			'value_name' => $param['value_name']
    		);
    		$res = Db::name('system_setting')->insertGetId($data);
    		if($res){
    			$this->success('添加成功','index');
    		}
    	}else{
    		return $this->fetch();
    	}
 
    }
    /**
     * 系统参数编辑
     * @adminMenu(
     *     'name'   => '系统参数编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '系统参数编辑',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit(){
    	$param = $this->request->param();
    	if(isset($param['id'])){
    		$data  =  Db::name('system_setting')->where('id',$param['id'])->find();
    		$this->assign('p',$data);
    		$this->fetch('edit');
    	}
    	// $data['id']=0;
    	$this->assign('p',$data);
    	return $this->fetch();
    }

    /**
     * 系统参数编辑
     * @adminMenu(
     *     'name'   => '系统参数编辑提交',
     *     'parent' => 'edit',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '系统参数编辑提交',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function update(){
        $param = $this->request->param();
        if(Request::isPost()){
            $data = array(
                'desc' => $param['desc'],
                'key_name' => $param['key_name'],
                'value_name' => $param['value_name']
            );
            // var_dump($data);
            // die;
            $res = Db::name('system_setting')->where('id',$param['id'])->update($data);
            if($res){
                $this->success('添加成功','index');
            }
        }
    }
}